package com.example.medicalzb.service;

import com.example.medicalzb.entity.MaterialEntity;
import com.example.medicalzb.mapper.MaterialMapper;
import com.example.medicalzb.model.MaterialModel;
import com.example.medicalzb.param.MaterialParam;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MaterialService {
    @Autowired
    private MaterialMapper materialMapper;

    public Msg getMaterialWithPage(MaterialParam materialParam) {
        if (materialParam.getPn() == null || materialParam.getPn() == 0){
            materialParam.setPn(1);
        }
        if (materialParam.getSize() == null || materialParam.getSize() == 0){
            materialParam.setSize(1);
        }
        PageHelper.startPage(materialParam.getPn(),materialParam.getSize());
        MaterialEntity materialEntity = new MaterialEntity();
        BeanUtils.copyProperties(materialParam,materialEntity);
        List<MaterialModel> list = materialMapper.getAllMaterial(materialEntity);
        PageInfo<MaterialModel> pageInfo = new PageInfo<>(list,5);
        return Msg.success().data("materialInfo",pageInfo);
    }

    public Msg saveMaterial(MaterialParam materialParam) {
        materialParam.setCreateTime(new DateTime().toDate());
        materialParam.setUpdateTime(new DateTime().toDate());
        MaterialEntity materialEntity = new MaterialEntity();
        BeanUtils.copyProperties(materialParam,materialEntity);
        int i = materialMapper.saveMaterial(materialEntity);
        materialEntity.setTotal((long) materialMapper.getAllMaterial(null).size());
        if (i > 0){
            Long num = materialEntity.getTotal() % 5 == 0 ? (materialEntity.getTotal() / 5) : ((materialEntity.getTotal() / 5) + 1);
            return Msg.success().mess("添加必备材料成功").data("numberOfAdd",i).data("pages",num);
        }
        return Msg.fail().mess("添加必备材料失败");
    }

    public Msg updateMaterial(Integer id, MaterialParam materialParam) {
        materialParam.setUpdateTime(new DateTime().toDate());
        materialParam.setId(id);
        MaterialEntity materialEntity = new MaterialEntity();
        BeanUtils.copyProperties(materialParam,materialEntity);
        System.out.println(materialEntity.getTitle());
        int i = materialMapper.updateMaterial(materialEntity);
        if (i > 0){
            return Msg.success().mess("修改必备材料信息成功").data("updateData",materialEntity);
        }
        return Msg.fail().mess("修改必备材料信息失败");
    }

    public Msg deleteMaterial(Integer id) {
        int i = materialMapper.deleteMaterialById(id);
        if (i > 0){
            return Msg.success().mess("删除必备材料信息成功").data("numberOfDelete",i);
        }
        return Msg.fail().mess("删除必备材料信息失败");
    }
}
